package org.nt4b.schemadoc.webgui.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;

import org.apache.log4j.Logger;
import org.nt4b.schemadoc.webgui.ConnectionConfiguration;
import org.nt4b.schemadoc.webgui.ConnectionProperties;
import org.nt4b.schemadoc.webgui.ConnectionPropertiesHolder;
import org.springframework.core.io.Resource;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class ConnectionConfigurationServiceImpl implements Serializable, ConnectionConfigurationService {
    private static final long serialVersionUID = 1156018874504283709L;
    
    protected Logger logger = Logger.getLogger(ConnectionConfigurationServiceImpl.class);
    
    private Resource configuration;
    
    public ConnectionConfigurationServiceImpl() {
        
    }
    
    /* (non-Javadoc)
     * @see org.schemautils.schemadoc.webgui.service.ConnectionConfigurationService#getConfiguration()
     */
    public Resource getConfiguration() {
        return configuration;
    }
    
    /* (non-Javadoc)
     * @see org.schemautils.schemadoc.webgui.service.ConnectionConfigurationService#setConfiguration(org.springframework.core.io.Resource)
     */
    public void setConfiguration(final Resource configuration) {
        this.configuration = configuration;
    }
    
    /* (non-Javadoc)
     * @see org.schemautils.schemadoc.webgui.service.ConnectionConfigurationService#load(org.schemautils.schemadoc.webgui.ConnectionConfiguration)
     */
    public void load(final ConnectionConfiguration config) {
        final ConnectionPropertiesHolder connectionPropertiesHolder = readConfiguration();
        if (connectionPropertiesHolder != null) {
            config.init(connectionPropertiesHolder.getConnections());
        } else {
            config.init(new ArrayList<ConnectionProperties>());
        }
    }
    
    private XStream getXStream() {
        final XStream xstream = new XStream(new DomDriver());
        xstream.alias("root", ConnectionPropertiesHolder.class);
        xstream.alias("connection", ConnectionProperties.class);
        return xstream;
    }
    
    /**
     * Read provided connection configuration
     * 
     * @return Connection properties holder
     */
    private ConnectionPropertiesHolder readConfiguration() {
        logger.info("Read connection configuration from: " + configuration);
        
        final XStream xstream = getXStream();
        
        InputStream is = null;
        try {
            is = configuration.getInputStream();
        } catch (final IOException e) {
            final String message = "Could not get input stream from provided resource " + configuration.toString() + ": " + e.getLocalizedMessage();
            logger.warn(message);
        }
        
        final ConnectionPropertiesHolder result;
        if (is != null) {
            result = (ConnectionPropertiesHolder) xstream.fromXML(is);
        } else {
            result = null;
        }
        
        return result;
    }
}
